package com.pactera.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.pactera.db.OracleDb;
import com.pactera.pojo.SmsLog;
import com.pactera.pojo.SmsPhone;
import com.pactera.utils.DateUtil;

public class OracleDBService {

	
	//获取某种状态的所有配置
	public List<SmsPhone> getSmsPhone(String type){
		String smsSql = "select * from sms_phone where sms_type="+type;
		return (List<SmsPhone>)OracleDb.query(smsSql, new BeanListHandler(
				SmsPhone.class));
	}
	//存储发送日志
	public void saveSmsLog(String phoneNum,String jobName,int state,String type){
		String logSql = "insert into sms_log (phone,jobname,state,sendtime,sms_type) values('"
				+ phoneNum
				+ "','"
				+ jobName.toString()
				+ "','"
				+ state + "','" + DateUtil.DateToStr(new Date()) + "','"
				+type+"')";
//		System.out.println(logSql);
		OracleDb.update(logSql);
	}
	//获取type1上次的短信
	public List<SmsLog> getLastType1(){
		String smsSql = "select * from sms_log where sms_type='1' and state=1 and sendtime="
				+ "(select max(sendtime) from sms_phone where sms_type='1' )";
		return (List<SmsLog>)OracleDb.query(smsSql, new BeanListHandler(
				SmsLog.class));
	}
	//获取今天type3的短信
	public List<SmsLog> getTodayType3(){
		String smsSql = "select * from sms_log where sms_type='3' and state=1 and sendtime like '%"
			   +DateUtil.DateToStr(new Date(),"yyyy-MM-dd")+"%'";
		return (List<SmsLog>)OracleDb.query(smsSql, new BeanListHandler(
				SmsLog.class));
	}

	//获取今天type4的短信
	public List<SmsLog> getTodayType4(){
		String smsSql = "select * from sms_log where sms_type='4' and state=1 and sendtime like '%"
				+DateUtil.DateToStr(new Date(),"yyyy-MM-dd")+"%'";
		return (List<SmsLog>)OracleDb.query(smsSql, new BeanListHandler(
				SmsLog.class));
	}


	//获取今天type6的短信发送日志
	public List<SmsLog> getTodayType6(){
		String smsSql = "select * from sms_log where sms_type='6' and state=1 and sendtime like '%"
				+DateUtil.DateToStr(new Date(),"yyyy-MM-dd")+"%' and ROWNUM < 2;";
		return (List<SmsLog>)OracleDb.query(smsSql, new BeanListHandler(
				SmsLog.class));
	}

	//获取今天需要执行的TYPE2
	public List<SmsPhone> getTodayType2(String now){
		String smsSql = "select * from sms_phone where sms_type='2' and quart_time>'"+now+"'";
		return (List<SmsPhone>)OracleDb.query(smsSql, new BeanListHandler(
				SmsPhone.class));
	}

}
